﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.QueueService
{
    /// <summary>
    /// 队列帮助类
    /// </summary>
    internal class QueueHelper
    {
        private static Dictionary<Type, QueueAttribute> queueMap = new Dictionary<Type, QueueAttribute>();
        private static QueueAttribute GetQueueAttribute(Type cmdType)
        {
            if (queueMap.ContainsKey(cmdType))
                return queueMap[cmdType];
            QueueAttribute attr = BaiTu.Common.Helpers.AttributeHelper.GetAttribute<QueueAttribute>(cmdType);

            if (attr == null)
            {
                string queueName = string.Format("{0}", cmdType.Name);
                attr = new QueueAttribute(queueName);
            }
            queueMap[cmdType] = attr;
            return attr;
        }
        /// <summary>
        /// 获得此类型的队列名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        internal static string GetQueueName<T>()
        {
            Type cmdType = typeof(T);
            return GetQueueName(cmdType);
        }

        /// <summary>
        /// 获得此类型的队列名
        /// </summary>
        /// <param name="cmdType"></param>
        /// <returns></returns>
        internal static string GetQueueName(Type cmdType)
        {
            QueueAttribute attr = GetQueueAttribute(cmdType);
            return string.Format("{0}", attr.QueueName);
        }
     

        /// <summary>
        /// 获得此类型的应答队列名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        internal static string GetResponseQueueName<T>()
        {
            Type cmdType = typeof(T);
            QueueAttribute attr = GetQueueAttribute(cmdType);
            return string.Format("{0}_R", attr.QueueName);
        }
    }
}
